package org.starapp.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class ListUtils {
	
	public static List<String> convert(String[] strings){
		if(ArrayUtils.isEmpty(strings)){
			return null;
		}
		List<String> list =new ArrayList<String>();
		for(String str:strings){
			list.add(str);
		}
		return list;
	}

	public static boolean isEmpty(List list){
		return (list==null||list.isEmpty());
	}
	/**
	 * 合并两List对象，并过滤重复数据
	 * @param firstList 基础List
	 * @param secondList, threeList,fourList需要处理的List
	 * @return
	 */

	public static List addListExpertExist(List firstList,List secondList,List threeList,List fourList)
	{
		if(secondList != null && secondList.size()>0){
			Iterator<Object> it=secondList.iterator();
			if(it != null){
				while(it.hasNext()){
					Object obj=it.next();
					if(firstList.contains(obj)){
						continue;
					}else{
						firstList.add(obj);
					}
				}
			}
		}
		if(threeList!=null &&threeList.size()>0){
			Iterator<Object> it=threeList.iterator();
			if(it !=null){
				while(it.hasNext()){
					Object obj=it.next();
					if(firstList.contains(obj)){
						continue;
					}else{
						firstList.add(obj);
					}
				}
			}
		}
		if(fourList!=null && fourList.size()>0){
			Iterator<Object> it=fourList.iterator();
			if(it !=null){
				while(it.hasNext()){
					Object obj=it.next();
					if(firstList.contains(obj)){
						continue;
					}else{
						firstList.add(obj);
					}
				}
			}
		}
		return firstList;
	}
	/**
	 * trim掉List中重复的记录
	 * @param enterList
	 * @return
	 */
	public static List trimList(List enterList){
		if(enterList==null || enterList.isEmpty()){
			return null;
		}
		List<Object> outerList=new ArrayList<Object>();
		Iterator<Object> it = enterList.iterator();
		while(it.hasNext()){
			Object o=it.next();
			if(enterList.indexOf(o)==enterList.lastIndexOf(o)){
				outerList.add(o);
			}
		}
		return outerList;
	}
	/**
	 * 显示List数据
	 * @param list
	 */
	public static void showListData(List list){
		if(list==null || list.isEmpty()){
			System.out.println("there is no data to show!");
		}else{
			Iterator<Object> it=list.iterator();
			while(it.hasNext()){
				Object o=(Object)it.next();
				System.out.println(o);
			}
		}
	}
	/**
	 * 将一个list<Object[]>转化成导出Excel所需要的list格式(list<list<String>>)
	 * 入口参数List<Object[]>
	 * 出口参数List<List>
	 * 注意！Object[].length=titles[].length       ;titles为导出Excel的标题头,也就是说标题头个数要等于列数
	 * @param list
	 * @return
	 */

	public static List convert(List list)
	{
		List resultList =  new ArrayList();
		try{
				if(list != null && list.size() > 0)
				{
					Iterator it = list.iterator();
					while(it.hasNext())
					{
						Object[] obj = (Object[])it.next();
						List tempList = null;
						if(obj != null && obj.length > 0)
						{
							for(int i=0;i<obj.length;i++)
							{
									tempList = new ArrayList();
									tempList.add(obj[i].toString());
							}
							resultList.add(tempList);
						}
					}
				}else{
					throw new RuntimeException("error ! in file "+ListUtils.class.getName()+" reason maybe paramter List is null");
				}
		}catch(Exception e)
		{
			throw new RuntimeException(e.getMessage());
		}
		return resultList;
		
	}
	
	/**
	 * 将一个list<Object[]>转化成导出Excel所需要的list格式(list<list<String>>)
	 * @param list :入口参数
	 * @param fromIndex : 开始索引,取每个list对象(Object[])从多少索引开始的数据(Object)
	 * @param toIndex   : 结束索引,取每个list对象(Object[])从多少索引结束的数据(Object)
	 * @return
	 */
	public static List convert(List list,int fromIndex,int toIndex)
	{
		List resultList = new ArrayList();
		List tempList = null;
		try{
				if(list != null && list.size() > 0)
				{
					Iterator it = list.iterator();
					while(it.hasNext())
					{
						Object[] obj = (Object[])it.next();
						tempList = new ArrayList();
						if(obj != null && obj.length > 0)
						{
							for(int i=0;i<obj.length;i++)
							{
								if(i >= fromIndex && i <= toIndex)
								{
									if(obj[i] != null){
										tempList.add(obj[i].toString());
									}else{
										tempList.add("");
									}	
									
								}
							}
							resultList.add(tempList);
						}
					}
					return resultList;
				}else{
					return null;
				}

		}catch(Exception e)
		{
			throw new RuntimeException(e.getMessage());
		}
		
	}
	/**
	 * 将一个list<list>转化成导出Excel所需要的list格式(list<list<String>>)
	 * @param list :入口参数
	 * @param fromIndex : 开始索引,取每个list对象(list)从多少索引开始的数据(Object)
	 * @param toIndex   : 结束索引,取每个list对象(list)从多少索引结束的数据(Object)
	 * @return
	 */
	public static List cut(List list,int fromIndex,int toIndex)
	{
		List resultList = new ArrayList();
		List tempList = null;
		try{
				if(list != null && list.size() > 0)
				{
					Iterator it = list.iterator();
					while(it.hasNext())
					{
						List obj = (List)it.next();
						tempList = new ArrayList();
						if(obj != null && obj.size() > 0)
						{
							for(int i=0;i<obj.size();i++)
							{
								if(i >= fromIndex && i <= toIndex)
								{
									if(obj.get(i) != null){
										tempList.add(obj.get(i).toString());
									}else{
										tempList.add("");
									}	
									
								}
							}
							resultList.add(tempList);
						}
					}
					return resultList;
				}else{
					return null;
				}

		}catch(Exception e)
		{
			throw new RuntimeException(e.getMessage());
		}
		
	}

	/**
	 * 以map中的key为匹配对象，用map中的value替换list中某列的所有字段
	 * @param list 入口参数
	 * @param position 要替换的字段的位置
	 * @param map  替换字段集
	 * @return  返回的list和原来的list为同一个
	 */
	public static List replace(List list,int position,Map map)
	{
		try{
			if(list != null && list.size() > 0)
			{
                for(int p=0;p<list.size();p++)
                {
					Object[] obj = (Object[])list.get(p);
					Object[] temp = null;
					if(obj != null && obj.length > 0)
					{
						temp = new Object[obj.length];
						list.remove(p);
						for(int i=0;i<obj.length;i++)
						{
							temp[i] = (i != position) ? obj[i] :  ((obj[i] == null)? null :((map.get(obj[i].toString()) == null)?(obj[i].toString()):map.get(obj[i].toString())));
						}
						list.add(p, temp);
					}
				}
			}
		}catch(Exception e){
			throw new RuntimeException(e.getMessage());
		}
		return list;
	}
	
	/**
	 * 以map中的key为匹配对象，用map中的value替换list中某列的所有字段
	 * @param list 入口参数
	 * @param position 要替换的字段的位置
	 * @param map  替换字段集
	 * @return  返回的list和原来的list为同一个
	 */
	public static List replaceIt(List list,int position,Map map)
	{
		try{
			if(list != null && list.size() > 0)
			{
                for(int p=0;p<list.size();p++)
                {
					List obj = (List)list.get(p);
					List temp = null;
					if(obj != null && obj.size() > 0)
					{
						temp = new ArrayList();
						list.remove(p);
						for(int i=0;i<obj.size();i++)
						{
							temp.add((i != position) ? obj.get(i) :  ((obj.get(i) == null)? null :((map.get(obj.get(i).toString()) == null)?(obj.get(i).toString()):map.get(obj.get(i).toString()))));
						}
						list.add(p, temp);
					}
				}
			}
		}catch(Exception e){
			throw new RuntimeException(e.getMessage());
		}
		return list;
	}
	
	/**
	 * 格式化LIST中的金额
	 * @param list 入口参数
	 * @param position 要替换的字段的位置
	 * @param map  替换字段集
	 * @return  返回的list和原来的list为同一个
	 */
	public static List formatMoney(List list,int position,String formatString)
	{
		String fmt = (formatString ==null||formatString.equals(""))?"#,##0.00":formatString;
		//String fmt="#,##0.00";
		DecimalFormat df = new DecimalFormat(fmt);
		try{
			if(list != null && list.size() > 0)
			{
                for(int p=0;p<list.size();p++)
                {
					Object[] obj = (Object[])list.get(p);
					Object[] temp = null;
					if(obj != null && obj.length > 0)
					{
						temp = new Object[obj.length];
						list.remove(p);
						for(int i=0;i<obj.length;i++)
						{
							temp[i] = (i != position) ? obj[i] :((obj[i]==null)?df.format(new BigDecimal(0)):df.format(new BigDecimal(obj[i].toString())));
						}
						list.add(p, temp);
					}
				}
			}
		}catch(Exception e){
			throw new RuntimeException(e.getMessage());
		}
		return list;
	}
	/**
	 * 格式化LIST中的金额
	 * @param list 入口参数
	 * @param position 要替换的字段的位置
	 * @param map  替换字段集
	 * @return  返回的list和原来的list为同一个
	 */
	public static List formatMoneyIt(List list,int position,String formatString)
	{
		String fmt = (formatString ==null||formatString.equals(""))?"#,##0.00":formatString;
		//String fmt="#,##0.00";
		DecimalFormat df = new DecimalFormat(fmt);
		try{
			if(list != null && list.size() > 0)
			{
                for(int p=0;p<list.size();p++)
                {
					List obj = (List)list.get(p);
					List temp = null;
					if(obj != null && obj.size() > 0)
					{
						temp = new ArrayList();
						list.remove(p);
						for(int i=0;i<obj.size();i++)
						{
							temp.add((i != position) ? obj.get(i) :((obj.get(i)==null)?df.format(new BigDecimal(0)):df.format(new BigDecimal(obj.get(i).toString()))));
						}
						list.add(p, temp);
					}
				}
			}
		}catch(Exception e){
			throw new RuntimeException(e.getMessage());
		}
		return list;
	}
	
	/**
	 * 修改样式
	 * @param list 入口参数
	 * @param position 要替换的字段的位置
	 * @param className
	 * @return  返回的list和原来的list为同一个
	 */
	private static List changStyle(List list,int position,String alignName)
	{
		try{
			if(list != null && list.size() > 0)
			{
				/*先替换掉需要特殊替换的字段*/
                for(int p=0;p<list.size();p++)
                {
					List obj = (List)list.get(p);
					List temp = null;
					if(obj != null && obj.size() > 0)
					{
						temp = new ArrayList();
						list.remove(p);
						for(int i=0;i<obj.size();i++)
						{
							temp.add((i != position) ?
										obj.get(i).toString()
										: 
										((obj.get(i) == null)?
										""
										:((obj.get(i).toString().trim().indexOf("<td")!=-1)?obj.get(i).toString():"<td nowrap align=\""+alignName+"\">"+
										(obj.get(i).toString())+"</td>")));
						}
						list.add(p, temp);
					}
				}
			}
		}catch(Exception e){
			throw new RuntimeException(e.getMessage());
		}
		return list;
	}
	private static String getAlign(String align)
	{
		return (align==null)?"left":(align.trim().equals("right")?"left":"right");
	}
	/**
	 * 批量修改样式
	 * @param list
	 * @param positions
	 * @param align
	 * @return
	 */
	public static List changStyleBatch(List list,String positions,String align)
	{
		align = (align==null||align.equals(""))?"right":align;
		if(list != null && list.size() >0)
		{
			if(positions != null && !positions.equals(""))
			{
				String[] s = positions.split(",");
				if(s != null && s.length>0)
				{
					try{
	
						 /* 先替换掉需要特殊替换的字段*/
							for(int i=0;i<s.length;i++)
							{
								int j = Integer.parseInt(s[i].toString().trim());
								changStyle(list,j,align);
							}
						 /*再替换其他字段*/	
							for(int p=0;p<list.size();p++)
							{
								List o = (List)list.get(p);
								if(o != null && o.size()>0)
								{
									for(int m=1;m<o.size();m++)
									{
										if(o.get(m).toString().trim().indexOf("<td")==-1)
										{
											changStyle(list,m,getAlign(align));
										}
									}
								}
							}
					}catch(Exception e){
						e.printStackTrace();
						throw new RuntimeException(e.getMessage());
					}
				}
			}else{
				 /*再替换其他字段*/	
				for(int p=0;p<list.size();p++)
				{
					List o = (List)list.get(p);
					if(o != null && o.size()>0)
					{
						for(int m=1;m<o.size();m++)
						{
							if(o.get(m).toString().trim().indexOf("<td")==-1)
							{
								changStyle(list,m,getAlign(align));
							}
						}
					}
				}
			}
		}
		return list;
	}
	/**
	 * 批量修改金额样式
	 * @param list
	 * @param positions
	 * @param align
	 * @return
	 */
	public static List formatMoneyBatch(List list,String positions,String formatString)
	{
		if(list != null && list.size() >0)
		{
			if(positions != null && !positions.equals(""))
			{
				String[] s = positions.split(",");
				if(s != null && s.length>0)
				{
					try{
						for(int i=0;i<s.length;i++)
						{
							int j = Integer.parseInt(s[i].toString().trim());
							list = formatMoneyIt(list,j,formatString);
						}
					}catch(Exception e){
						e.printStackTrace();
						throw new RuntimeException(e.getMessage());
					}
				}
			}
		}
		return list;
	}
}
